{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "inputWidgets": {},
     "nuid": "d2616264-8177-4893-ac16-f123fd727125",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "# restaurant-orders\n",
    "\n",
    "从订单列表中统计 \"Plain Papadum\",\"Butter Chicken\",\"Bengal Fry Fish\" 这三样产品的销售总数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "inputWidgets": {},
     "nuid": "b7ef5704-7fec-4fdf-8146-0c86fd06b577",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "import datetime\n",
    "\n",
    "# 读取数据文件\n",
    "fileRdd = sc.textFile(\"/mnt/databrickscontainer1/restaurant-1-orders.csv\",4)\n",
    "\n",
    "# 广播变量，减少内存中相同数据的份数\n",
    "items = sc.broadcast([\"Plain Papadum\",\"Butter Chicken\",\"Bengal Fry Fish\"])\n",
    "# 累加器\n",
    "totals = sc.accumulator(0)\n",
    "\n",
    "# 将订单中的标题行去掉\n",
    "dataRdd = fileRdd.map(lambda x: x.split(\",\")).filter(lambda x: x[0] != \"Order Number\")\n",
    "\n",
    "# print(dataRdd.map(lambda x:x[2]).distinct().collect())\n",
    "# print(dataRdd.map(lambda x: x[2] in items.value).collect())\n",
    "\n",
    "# 使用广播变量，过滤出列表中列出的产品及销售数量\n",
    "saleRdd = dataRdd.filter(lambda x: x[2] in items.value).map(lambda x: (x[2],int(x[3])))\n",
    "\n",
    "# 仅保留销售数量\n",
    "quantityRdd = saleRdd.map(lambda x: x[1])\n",
    "\n",
    "# 缓存一下\n",
    "quantityRdd.cache()\n",
    "\n",
    "# 使用reduce计算数量\n",
    "print(quantityRdd.reduce(lambda a,b: a + b))\n",
    "# 使用fold计算数量\n",
    "print(quantityRdd.fold(0, lambda a,b: a + b))\n",
    "# 使用累加器计算数量\n",
    "print(totals)\n",
    "def sum_func(data):\n",
    "    global totals\n",
    "    totals += data\n",
    "\n",
    "quantityRdd.foreach(sum_func)\n",
    "print(totals)"
   ]
  }
 ],
 "metadata": {
  "application/vnd.databricks.v1+notebook": {
   "dashboards": [],
   "language": "python",
   "notebookMetadata": {},
   "notebookName": "restaurant-orders",
   "notebookOrigID": 2180139033493902,
   "widgets": {}
  },
  "kernelspec": {
   "display_name": "",
   "name": ""
  },
  "language_info": {
   "name": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
